import React, {FC, useState, useEffect, useRef} from 'react';
import {
  createBottomTabNavigator,
  BottomTabBarProps,
} from '@react-navigation/bottom-tabs';
// import {HomeOutline} from '@fruits-chain/icons-react-native';
import {TransitionPresets} from '@react-navigation/stack';
import {
  Text,
  View,
  StyleSheet,
  TouchableOpacity,
  Image,
  TextInput,
} from 'react-native';
import {Button} from '../../components';
import Toast from 'react-native-simple-toast';
import storage from '../../utils/storage';
import styles from './style';

type Props = {
  navigation: any;
};

const Public: FC<Props> = ({navigation}) => {
  const [formData, setFormData] = useState({
    title: '',
    content: '',
  });

  const submitHandle = () => {
    if (!formData.title) return Toast.show('标题不能为空~', Toast.LONG);
    let _random = Math.random();
    let _publicid = `public-${_random}`
    storage.save({
      key: `public`,
      id:_publicid,
      data: {
        ...formData,
        time: +new Date(),
        id: _publicid,
      },
    });
    Toast.show('提交成功~', Toast.LONG);


    setTimeout(() => {
      navigation.goBack();
    }, 2000);

  };

  return (
    <View style={styles.publicwrapper}>
      <View>
        <Text style={styles.publictitle}>标题：</Text>
        <View style={styles.inputwrapper}>
          <TextInput
            placeholder="请输入标题"
            value={formData.title}
            onChangeText={e => {
              setFormData({
                ...formData,
                title: e,
              });
            }}
          />
        </View>
      </View>
      <View style={{marginTop: 20}}>
        <Text style={styles.publictitle}>备注：</Text>
        <View style={styles.inputwrapper}>
          <TextInput
            placeholder="请输入备注"
            multiline={true}
            numberOfLines={4}
            value={formData.content}
            onChangeText={e => {
              setFormData({
                ...formData,
                content: e,
              });
            }}
          />
        </View>
      </View>
      <View style={styles.btnbox}>
        <Button
          onPress={submitHandle}
          style={styles.btn}
          border={true}
          title="提交"></Button>
      </View>
    </View>
  );
};

export default Public;
